package com.intct.hbase.dml;

import com.intct.hbase.HBaseConnection;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Table;

import java.io.IOException;

/**
 * @author gufg
 * @since 2025-11-04 11:12
 */
public abstract class AbsDml {

    /*
    实现方法
     */
    public void start(String namespace, String tName) throws IOException {
        // 建立连接
        Connection connection = HBaseConnection.connection;

        // TableName实例
        TableName tableName = TableName.valueOf(namespace, tName);

        // 获取Admin实例
        Admin admin = connection.getAdmin();

        Table table = null;

        try {
            // 判断表存在时
            if (admin.tableExists(tableName)) {
                // 获取Table实例
                table = connection.getTable(tableName);

                // 子类型实现
                execCommand(table);
            }
        } catch (IOException e) {
            System.out.println("操作数据失败！");
        } finally {
            // 关闭资源
            if (admin != null) {
                admin.close();
            }
            if (table != null) {
                table.close();
            }
        }

    }

    /*
    抽象方法
     */
    abstract void execCommand(Table table) throws IOException;
}
